/**
 * 初始化优惠券详情对话框
 */
var WzCouponInfoDlg = {
    userCode:"",
    userInfoCode:"",
    wzCouponInfoData : {},
    validateFields: {
        title: {
            validators: {
                notEmpty: {
                    message: '标题不能为空！'
                }

            }
        },
        subremark: {
            validators: {
                notEmpty: {
                    message: '详细备注不能为空！'
                }

            }
        },
        fileId: {
            validators: {
                notEmpty: {
                    message: '封面图片id！'
                }

            }
        },
        merchantAddress: {
            validators: {
                notEmpty: {
                    message: '商家地址！'
                }

            }
        },
        merchantTel: {
            validators: {
                notEmpty: {
                    message: '商家电话！'
                }

            }
        },
        quantity: {
            validators: {
                notEmpty: {
                    message: '数量不能为空！'
                },
                regexp: {
                    regexp: /^[0-9]+$/,
                    message: '请输入正整数！'
                }
            }
        },
        beginTime: {
            validators: {
                notEmpty: {
                    message: '请输入开始时间！'
                }

            }
        },
        endTime: {
            validators: {
                notEmpty: {
                    message: '请输入结束时间！'
                }

            }
        },
    }
};

/**
 * 清除数据
 */
WzCouponInfoDlg.clearData = function() {
    this.wzCouponInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
WzCouponInfoDlg.set = function(key, val) {
    this.wzCouponInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
WzCouponInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
WzCouponInfoDlg.close = function() {
    parent.layer.close(window.parent.WzCoupon.layerIndex);
}

/**
 * 收集数据
 */
WzCouponInfoDlg.collectData = function() {
    this
    .set('couponId')
    .set('title')
    .set('coverUrl')
    .set('userInfoId')
    .set('beginTime')
    .set('endTime')
    .set('quantity')
    .set('remark')
    .set('type')
    .set('status')
    .set('createTime')
    .set('merchantAddress')
    .set('merchantTel')
    .set('fileId')
    .set('subtitle')
    .set('subremark');
}



/**
 * 提交添加
 */
WzCouponInfoDlg.addSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var beginTime=$("#beginTime").val();
    var endTime=$("#endTime").val();
    if(beginTime!=null&&endTime!=null&&beginTime>endTime){
        Feng.error("添加失败！开始时间不能晚于结束时间！");
        $("#beginTime").css("background", "pink");
        $("#beginTime").focus();
        return;
    }
    if(beginTime!=null&&endTime!=null&&beginTime==endTime){
        Feng.error("添加失败！开始时间和结束时间相等！");
        $("#beginTime").css("background", "pink");
        $("#beginTime").focus();
        return;
    }
    var selected = $('#WzMerchantInfoTable').bootstrapTable('getSelections');
    if (selected[0] != null) {
        var userInfoId=selected[0]['userInfoId'];
        WzCouponInfoDlg.userInfoId=userInfoId;
        var userCode=selected[0]['userCode'];
        WzCouponInfoDlg.userCode=userCode;
    }else{
        Feng.error('请选择商户信息!');
        return false;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/wzCoupon/add", function(data){
        if(data.code == 100){
            Feng.error(data.message);
        }else if(data.code == 200){
            Feng.success("添加成功!");
            window.parent.WzCoupon.table.refresh();
            WzCouponInfoDlg.close();
        }
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.wzCouponInfoData);
    ajax.set("userCode",this.userCode);
    ajax.set("userInfoId",this.userInfoId);
    ajax.start();

}

/**
 * 提交修改
 */
WzCouponInfoDlg.editSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var beginTime=$("#beginTime").val();
    var endTime=$("#endTime").val();
    if(beginTime!=null&&endTime!=null&&beginTime>endTime){
        Feng.error("修改失败！开始时间不能晚于结束时间！");
        $("#beginTime").css("background", "pink");
        $("#beginTime").focus();
        return;
    }
    if(beginTime!=null&&endTime!=null&&beginTime==endTime){
        Feng.error("修改失败！开始时间和结束时间相等！");
        $("#beginTime").css("background", "pink");
        $("#beginTime").focus();
        return;
    }
    var selected = $('#WzMerchantInfoTable').bootstrapTable('getSelections');
    if (selected[0] != null) {
        var userInfoId=selected[0]['userInfoId'];
        WzCouponInfoDlg.userInfoId=userInfoId;
        var userCode=selected[0]['userCode'];
        WzCouponInfoDlg.userCode=userCode;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/wzCoupon/update", function(data){
        if(data.code == 100){
            Feng.error(data.message);
        }else if(data.code == 200){
            Feng.success("修改成功!");
            window.parent.WzCoupon.table.refresh();
            WzCouponInfoDlg.close();
        }
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.wzCouponInfoData);
    if (selected[0] != null) {
        ajax.set("userCode", this.userCode);
        ajax.set("userInfoId", this.userInfoId);
    }
    ajax.start();
}
/**
 * 验证数据是否为空
 */
WzCouponInfoDlg.validate = function () {
    $('#WzCouponInfoForm').data("bootstrapValidator").resetForm();
    $('#WzCouponInfoForm').bootstrapValidator('validate');
    return $("#WzCouponInfoForm").data('bootstrapValidator').isValid();
}
$(function() {
    Feng.initValidator("WzCouponInfoForm", WzCouponInfoDlg.validateFields);
});
